<script type="text/javascript" defer="defer">
<!--
	formObj = new form('tableBuilder');
	function constraint_onclick(constraint)
	{
		var checked = null;
		if (constraint == 'unique')
		{
			checked = formObj.getChecked('constraint_unique');
			formObj.setChecked('constraint_primary', false);
			formObj.setValue('constraint', checked ? 'unique' : '');

		}
		else if (constraint == 'primary')
		{
			checked = formObj.getChecked('constraint_primary');
			formObj.setChecked('constraint_unique', false);
			formObj.setValue('constraint', checked ? 'primary' : '');
		}
	}
    function clearForm()
    {
		formObj.setValue('columnName', '');
		formObj.setSelectedIndex('columnType', 0);
		formObj.setValue('columnLength', '');
		formObj.setValue('columnDefault', '');
		formObj.setChecked('null', false);
		formObj.setValue('constraint', '');
		formObj.setChecked('constraint_primary', false);
		formObj.setChecked('constraint_unique', false);
		formObj.setValue('addColumn', '<?=lang('Add Column')?>');
		formObj.setSelectedIndex('columnList', -1);
    }
    function addColumn_onclick()
    {
    	var value = new Array();
    	var text  = '';
    	value[0] = formObj.getValue('columnName');
		value[1] = formObj.getValue('columnType');
		value[2] = formObj.getValue('columnLength');
		value[3] = formObj.getValue('columnDefault');
		value[4] = formObj.getValue('null');
		value[5] = formObj.getValue('constraint');
		if (value[0] == '') {
			alert('<?php echo lang('Please enter a column name!')?>');
			return false;
		}
        text += value[0];

        // Column type
		text += ' ' + value[1];

        // Column length
		if (value[2] != '') {
			text += ' (' + value[2] + ')';
		}

        // Default value
		if (value[3] != '') {
			text += " DEFAULT '" + value[3] + "'";
		}

        // Not null/null
		if (!value[4]) {
			text += ' NOT NULL';
		}

        // Primary Key/Unique constraint
		if (value[5] == 'unique') {
			text += ' UNIQUE';

		} else if (value[5] == 'primary') {
			text += ' PRIMARY KEY';
		}

        value = value.join(':');

        // If we're doing an addition, do this
        if (formObj.numSelected('columnList') == 0) {
	        formObj.addOptionToSelect('columnList', new Option(text, value));
	    } else {
	    	formObj.setSelectedOptionValue('columnList', value);
			formObj.setSelectedOptionText('columnList', text);

			// Now the column list has been updated, unselect the entry
			// and reset the add/update button
			formObj.setSelectedIndex('columnList', -1);
	    }

	    // Reset the form to blank
	    clearForm();
    }

    /**
    * Handles when the Remove Column button is clicked
    */
    function removeColumn_onclick()
    {
    	formObj.getSelectedIndex('columnList');
    	if (formObj.numSelected('columnList') == 1) {
			formObj.removeOptionFromSelect('columnList', formObj.getSelectedIndex('columnList'));
    	}

    	clearForm();

    	return false;
    }

    /**
    * Handles when a column is selected in the column list
    */
    function columnList_onchange(selectObj)
    {
    	var value = formObj.getValue('columnList');
		value = value.split(':');

    	// Set the value of the add button to update
    	formObj.setValue('addColumn', '<?php echo lang('Update Column')?>');

    	// Set all the column inputs
    	formObj.setValue('columnName', value[0]);
    	formObj.setValue('columnType', value[1]);
    	formObj.setValue('columnLength', value[2]);
    	formObj.setValue('columnDefault', value[3]);
    	formObj.setChecked('null', value[4] == '1');
    	formObj.setValue('constraint', value[5]);
    	formObj.setChecked('constraint_primary', value[5] == 'primary');
    	formObj.setChecked('constraint_unique', value[5] == 'unique');
    }

    /**
    * Handles the move up buttons onclick event
    */
    function moveUp_onclick()
    {
    	var selectedIndex = formObj.getSelectedIndex('columnList');

		if (selectedIndex > 0) {
			formObj.swapOptions('columnList', selectedIndex, selectedIndex - 1);
			formObj.setSelectedIndex('columnList', selectedIndex - 1);
		}
    }

    /**
    * Handles the move down buttons onclick event
    */
    function moveDown_onclick()
    {
    	var selectedIndex = formObj.getSelectedIndex('columnList');

		if (selectedIndex < (formObj.elements['columnList'].options.length - 1)) {
			formObj.swapOptions('columnList', selectedIndex, selectedIndex + 1);
			formObj.setSelectedIndex('columnList', selectedIndex + 1);
		}
    }

    /**
    * Handles when submit button is clicked
    */
    function addTable_onclick()
    {
    	if (formObj.numOptions('columnList') == 0) {
    		alert('<?php echo lang('You must specify some columns!')?>');
    		return false;
    	}

    	if (formObj.getValue('tableName').replace(/\s+/, '') == '') {
    		alert('<?php echo lang('Please enter a table name!')?>');
    		return false;
    	}

    	// Condense column list down into hidden input
    	var columns = new Array();
    	var columnListOptions = formObj.elements['columnList'].options;
    	for (var i=0; i<columnListOptions.length; ++i) {
			columns[columns.length] = columnListOptions[i].value;
    	}

    	formObj.setValue('columns', columns.join('|'));
    }
// -->
</script>
